package es.udc.pfc.davidparedes.model.unavailable;

import java.util.Calendar;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Version;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import es.udc.pfc.davidparedes.model.vendor.Vendor;

@Entity
public class Unavailable {
	
	private long unavailableId;
	private Calendar start;
	private Calendar end;
	private long duration;
	private Vendor vendor;
	private long version;
	
	public Unavailable() {
	}
	
	public Unavailable(Calendar start, Calendar end, Vendor vendor) {
		this.start = start;
		this.end = end;
		this.vendor = vendor;
		
		if (start.before(end)) {
			this.duration = (end.getTimeInMillis()-start.getTimeInMillis());
		} else {
			this.duration = 0;
		}
	}
	
	@Column (name = "id")
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public long getUnavailableId() {
		return unavailableId;
	}
	
	public void setUnavailableId(long unavailableId) {
		this.unavailableId = unavailableId;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	public Calendar getStart() {
		return start;
	}
	
	public void setStart(Calendar start) {
		this.start = start;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	public Calendar getEnd() {
		return end;
	}
	
	public void setEnd(Calendar end) {
		this.end = end;
	}
	
	public long getDuration() {
		return duration;
	}
	
	public void setDuration(long duration) {
		this.duration = duration;
	}
	
	@ManyToOne(optional=false, fetch=FetchType.LAZY)
	@JoinColumn(name="vendor")
	public Vendor getVendor() {
		return vendor;
	}
	
	public void setVendor(Vendor vendor) {
		this.vendor = vendor;
	}
	
	@Version
	public long getVersion() {
		return version;
	}
	
	public void setVersion(long version) {
		this.version = version;
	}
	
	@Override
	public String toString() {
		return ("Unavailable [unavailableId=" + unavailableId + ", start=" + start
				+ ", end=" + end + ", duration=" + duration + ", vendor=" + vendor
				+ ", version=" + version + "]");
	}

}
